package com.weipu.common.facade.support.service.task;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import com.weipu.common.DXApplication;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.config.ConfigUtil;
import com.weipu.common.facade.model.status.AppRunStatus;
import com.weipu.common.facade.model.task.TaskType;
import com.weipu.common.facade.support.service.TaskService;
import com.weipu.common.util.Logger;

/**
 * 数据清理任务
 */
public class ClearDataTask extends BaseServiceTask<Boolean>
{
    private TaskType type;
    
    public ClearDataTask(TaskType type)
    {
        this.type = type;
    }
    
    /**
     * 
     */
    private static final long serialVersionUID = 8419058866945901566L;
    
    @Override
    protected boolean executeRequest()
    {
        return true;
    }
    
    @Override
    protected Boolean execute()
    {
        //清理本地所有数据
        if (type == TaskType.CLEAR_DB_CACHE)
        {
            clearDBCache();
        }
        //程序退出数据清理请求
        else if (type == TaskType.CLEAR_DB_AFTER_EXIT)
        {
            exitApp();
        }
        return true;
    }
    
    //程序退出
    private void exitApp()
    {
        //清理本地数据
        clearDBCache();
        
        //标志位: 程序运行结束
        ConfigUtil.getInstance().getConfig().getStartupConfig().setRunStatus(AppRunStatus.FINISH);
        
        //关闭后台任务
        DXApplication.getContext().stopService(new Intent(DXApplication.getContext(), TaskService.class));
        
        //当前程序下服务、Activity销毁        
        DXApplication.getContext().onTerminate();
        
        // 退出此包下的所有程序
        ActivityManager act = (ActivityManager)DXApplication.getContext().getSystemService(Context.ACTIVITY_SERVICE);
        act.restartPackage(DXApplication.getContext().getPackageName());
        
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
    
    //清理内存及数据库缓存数据
    private void clearDBCache()
    {
        //清理内存缓存
        clearMemoryCache();
        
        //清理本地数据库缓存
        clearLocalCache();
    }
    
    //清理内存数据
    private void clearMemoryCache()
    {
        //TODO 清理内存缓存, GlogbalConfig todo
    }
    
    //清理本地数据库
    private void clearLocalCache()
    {
                //Banner数据
//                try
//                {
//                    ProviderFactory.createBannerServiceDBProvider().clearBanner();
//                }
//                catch (Exception e)
//                {
//                    Logger.w("clearBanner error. ", e);
//                }
        
                //栏目数据
                try
                {
                    ProviderFactory.createCategoryServiceDBProvider().clearCategory();
                }
                catch (Exception e)
                {
                    Logger.w("clearCategory error. ", e);
                }
        
        //首页产品数据服务器有缓存超时机制不用手动清除产品数据
//        try
//        {
//            ProviderFactory.createProductDBProvider().clearProduct();
//        }
//        catch (Exception e)
//        {
//            Logger.w("clearProduct error. ", e);
//        }
    }
    
    @Override
    public boolean equals(Object o)
    {
        if (!(o instanceof ClearDataTask))
        {
            return false;
        }
        
        return this.toString().equals(o.toString());
    }
    
    //    equals 返回 true的链各个对象则hashcode必须相等
    @Override
    public int hashCode()
    {
        return toString().hashCode();
    }
    
    //提供Equals对比，单例任务(同一时间只需运行一个)
    @Override
    public String toString()
    {
        return "ClearDataTask";
    }
    
    @Override
    protected void executeResponse(Boolean result)
    {
        super.executeResponse(result);
    }
    
}
